<html>
<head>
<script language="javascript" 
        src="js/WebUIValidation.js">
</script>

<script language="JScript">
<!--
// client-side validation function for CustomValidator
function ClientValidate(objSource, strValue) {
  var blnValid = true;
  var intNumber = strValue;
  if (intNumber % 2 == 1) {
    var intDivisor = Math.floor(intNumber / 3);
    if (intDivisor > 2) {
      for (var i = 3; i <= intDivisor; i = i + 2) {
        if (intNumber % intDivisor == 0) {
          blnValid = false;
          break;
        }
      }
    }
    else
      blnValid = false;
  }
  else
    blnValid = false;
  return blnValid;
}
//-->
</script>
<!-- #include file="style.inc" -->
</head>
<body bgcolor="#ffffff" >
<!--------------------------------------------------------------------------->
<span class="heading">ASP.NET Validation Controls</span> &nbsp; &nbsp; &nbsp;
<form runat="server">
 <table border="0">
 <tr>
  <td align="right">A Required Value:</td>
  <td><input type="text" id="txtRequired" size="20" runat="server" />
      <asp:RequiredFieldValidator id="valRequired" runat="server"
           ControlToValidate="txtRequired"
           ErrorMessage="* You must enter a value in the first text box"
           Display="dynamic">
           *
      </asp:RequiredFieldValidator>
  </td>
 </tr><tr>
  <td align="right">The Same Value Again:</td>
  <td><input type="text" id="txtCompare" size="20" runat="server" />
      <asp:CompareValidator id="valCompare" runat="server"
           ControlToValidate="txtCompare"
           ControlToCompare="txtRequired"
           Operator="Equal"
           ErrorMessage="* You must enter the same value in the second text box"
           Display="dynamic">
           *
      </asp:CompareValidator>
  </td>
 </tr><tr>
  <td align="right">A Date after 3rd March 2001:</td>
  <td><input type="text" id="txtCompareDate" size="10" runat="server" />
      <span class="cite">Tip: use something like "10/10/02"</span>
      <asp:CompareValidator id="valCompareDate" runat="server"
           ControlToValidate="txtCompareDate"
           ValueToCompare="3/3/2001"
           Operator="GreaterThan"
           Type="Date"
           ErrorMessage="* The Date must be later than 3rd March 2001"
           Display="dynamic">
           *
      </asp:CompareValidator>
  </td>
 </tr><tr>
  <td align="right">A Number between 1 and 10:</td>
  <td><input type="text" id="txtRange" size="5" runat="server" />
      <asp:RangeValidator id="valRange" runat="server"
           ControlToValidate="txtRange"
           MaximumValue="10"
           MinimumValue="1"
           Type="Integer"
           ErrorMessage="* The Number must between 1 and 10"
           Display="dynamic">
           *
      </asp:RangeValidator>
  </td>
 </tr><tr>
  <td align="right">Match Expression "<b>.*@.*\..*</b>":</td>
  <td><input type="text" id="txtRegExpr" size="40" runat="server" />
      <span class="cite">Tip: enter a valid email address</span>
      <asp:RegularExpressionValidator id="valRegExpr" runat="server"
           ControlToValidate="txtRegExpr"
           ValidationExpression=".*@.*\..*"
           ErrorMessage="* Your entry does not match the regular expression"
           Display="dynamic">
           *
      </asp:RegularExpressionValidator>
  </td>
 </tr><tr>
  <td align="right">A Prime Number over 100:</td>
  <td><input type="text" id="txtCustom" size="5" runat="server" />
      <span class="cite">Tip: 197 will work</span>
      <asp:CompareValidator id="valComparePrime" runat="server"
           ControlToValidate="txtCustom"
           ValueToCompare="100"
           Operator="GreaterThan"
           Type="Integer"
           ErrorMessage="* The Prime Number must be greater than 100"
           Display="dynamic">
           *
      </asp:CompareValidator>
      <asp:CustomValidator id="valCustom" runat="server"
           ControlToValidate="txtCustom"
           ClientValidationFunction="ClientValidate"
           OnServerValidate="ServerValidate"
           ErrorMessage="* Your knowledge of prime numbers is less than optimal"
           Display="dynamic">
           *
      </asp:CustomValidator>
  </td>
 </tr>
 </table>

<asp:ValidationSummary id="valSummary" runat="server"
     HeaderText="<b>The following errors were found:</b>"
     ShowSummary="true" DisplayMode="List" />

<hr />
Validation Enabled:
<asp:DropDownList AutoPostBack="True" id="lstEnabled" runat="server">
<asp:ListItem Text="True" /><asp:ListItem Text="False" /></asp:DropDownList>
&nbsp; &nbsp;EnableClientScript:
<asp:DropDownList AutoPostBack="True" id="lstClientScript" runat="server">
<asp:ListItem Text="True" /><asp:ListItem Text="False" /></asp:DropDownList>
&nbsp; &nbsp;ShowMessageBox:
<asp:DropDownList AutoPostBack="True" id="lstMessageBox" runat="server">
<asp:ListItem Text="False" /><asp:ListItem Text="True" /></asp:DropDownList>
<p />&nbsp; &nbsp;
<input type="submit" id="cmdConfirm" value="Submit" runat="server" onserverclick="ConfirmEntry">&nbsp; &nbsp;
<input type="submit" id="cmdCancel" value="Cancel" runat="server" onserverclick="CancelEntry">&nbsp; &nbsp;
</form>
<div id="outMessage" EnableViewState="False" runat="server" />
<script language="VB" runat="server">

Sub Page_Load()

  Dim objValidator As BaseValidator
  For Each objValidator In Page.Validators
    objValidator.Enabled = lstEnabled.SelectedItem.Text
    objValidator.EnableClientScript = lstClientScript.SelectedItem.Text
  Next
  valSummary.ShowMessageBox = lstMessageBox.SelectedItem.Text

End Sub

Sub ServerValidate(objSource As Object, objArgs As ServerValidateEventArgs)
  Dim blnValid As Boolean = True
  Try
    Dim intNumber As Integer = objArgs.Value
    'check that it's an odd number
    If intNumber Mod 2 = 1 Then
      'get the largest possible divisor
      Dim intDivisor As Integer = intNumber \ 3
      If intDivisor > 2 Then
         Dim intLoop As Integer
         'check using each divisor in turn
         For intLoop = 3 To intDivisor Step 2
           If intNumber Mod intDivisor = 0 Then
             blnValid = False
             Exit For
           End If
         Next
      Else
        blnValid = False
      End If
    Else
      blnValid = False
    End If
  Catch objError As Exception
    blnValid =False
  Finally
    objArgs.IsValid = blnValid
  End Try
End Sub

Sub ConfirmEntry(objSender As Object, objArgs As EventArgs)
  outMessage.InnerHtml = "Page.IsValid returned <b>" & Page.IsValid & ".</b>"
End Sub

Sub CancelEntry(objSender As Object, objArgs As EventArgs)
  outMessage.InnerHtml = "Validation was not carried out."
End Sub

</script>
<!--------------------------------------------------------------------------->
<!-- #include file="foot.inc" -->
</body>
</html>

